<?php

/**
 * alltosun.com 终端设备控制器 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 刘德洪 (Liudh@alltosun.com) $
 * $Date: 20151124 $
 * $Id$
 */
class Action
{
    private $per_page = 100;

    /**
     * 显示列表
     */
    public function index() {

        $page_no = $status = $search_filter = $type = $is_excel = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'status' => -1,
            'search_filter' => array(),
            'type' => 'ALL',
            'is_excel' => 0,
        )));
        //os设备添加状态。创建表做奖励记录。不重复奖励。
        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();

        if (-1 != $status) {
            $filter['status'] = $status;
        } else {
            $filter['status >= '] = '0';
        }

        if (isset($search_filter['device_no']) && $search_filter['device_no']) {
            $filter["code LIKE "] = '%' . $search_filter['device_no'] . '%';
        }

        if ($type != 'ALL') {
            $filter['type'] = $type;
        }

        $region_list = area_helper::get_code_area_list();
        $device_list = get_data_list('device', $filter, 'ORDER BY id DESC', $page_no, $this->per_page);
        if ($device_list) {
            foreach ($device_list as $k => $v) {
                $device_list[$k]['device_no'] = $v['code'];
                $relation_info = _model('store_device_relation')->read(array('dev_id' => $v['id']), 'order by `id` desc');
                $store_info = array();
                if ($relation_info) {
                    $store_info = store_helper::get_store($relation_info['store_id']);
					if($v['type'] = 'OS'){
						$bind = $cash_info = array();
						$bind['dev_id'] =$v['id'];
						$bind['agency_id'] =$store_info['agency_id'];
		                $cash_info = _model('bind_device_cash')->read($bind, 'order by `id` desc');
		                if($cash_info){
		                	$device_list[$k]['cash_time'] = $cash_info['add_time'];
		                }
					}
                    $device_list[$k]['agency_id'] = $store_info['agency_id'];
                    $device_list[$k]['agency_name'] = user_helper::get_realname_by_id($store_info['agency_id']);
                    if ($store_info) {
                        $store_info['province'] = @$region_list[$store_info['province_id']];
                        $store_info['city'] = @$region_list[$store_info['city_id']];
                        $store_info['county'] = @$region_list[$store_info['district_id']];
                    }
                }
                $device_list[$k]['store_info'] = $store_info;
            }
        }
        Response::assign('device_list', $device_list);
        Response::assign('type', $type);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/device_list.html');
    }

    /**
     * 修改状态
     */
    public function change_status() {
        $id = Request::getParam('id', 0);
        $status = Request::getParam('status', -1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $info = _uri('store_machine', $id);
        if ($status == $info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        _model('store_machine')->update($id, array('status' => $status));

        return 'ok';
    }

    /**
     * 返现
     */
    public function return_money_binding() {
        $id = Request::getParam('id', 0);
        $agency_id = Request::getParam('agency_id', '');
        if (!$id || !$agency_id) {
            return array('没有设备id或合伙人信息', 'error', AnUrl('device/admin'));
        }
		$bind['dev_id'] =$id;
		$bind['agency_id'] =$agency_id;
        $relation_info = get_data_list('bind_device_cash', $bind);
		if($relation_info){
            return array('此设备已通过审核并返现', 'error', AnUrl('device/admin'));
		}else{
	        $data = array();
	        $data['user_id'] = $agency_id;
	        $data['admin_id'] = user_helper::get_user_id();
	        $data['user_money'] = 100;
	        $data['res_id'] = 20;
	        $data['extra'] = '绑定设备返现金￥100';
	        $user_info = _uri('user', $data['user_id']);
	        if ($user_info) {
	            $ret = user_money_helper::change_user_money($data);
	            if (!$ret) {
	                return '返现失败!';
	            }
	        	$record_list = _model('bind_device_cash')->create(array('dev_id' => $id,'agency_id' =>$agency_id,'amount'=>100));
	        	return '此设备已成功返现';
	        } else {
	            return '未找到此用户信息!';
	        }
		}
    }


    /**
     * 获取设备好记录列表
     */
    public function get_device_no_record() {
        $id = Request::getParam('id', 0);
        $device_no = Request::getParam('device_no', '');
        if (!$id || !$device_no) {
            return array('没有设备id或设备号', 'error', AnUrl('device/admin'));
        }

        $device_id = _model('device')->read(array('code' => $device_no));
        $device_history = _model('store_device_record')->getList(array('dev_id' => $device_id['id']));
        //        $record_list =  get_data_list('store_device_relation', array('dev_id' => $id));

        if ($device_history) {
            foreach ($device_history as $k => $v) {
                $store_info = array(
                    'province' => '',
                    'city' => '',
                    'county' => '',
                );

                if ($v['store_id']) {
                    $store_info = store_helper::get_store($v['store_id']);
                    if ($store_info) {
                        $province_info = area_helper::get_area_info(@$store_info['province_id']);
                        $city_info = area_helper::get_area_info(@$store_info['city_id']);
                        $county_info = area_helper::get_area_info(@$store_info['district_id']);
                        $store_info['province'] = @$province_info['area'];
                        $store_info['city'] = @$city_info['area'];
                        $store_info['county'] = @$county_info['area'];
                    }
                }

                $device_history[$k]['store_info'] = $store_info;
            }
        }

        //        if ($record_list) {
        //        	foreach ($record_list as $k => $v) {
        //                $store_info = store_helper::get_store($v['store_id']);
        //                if ($store_info) {
        //                    $province_info = area_helper::get_area_info(@$store_info['province_id']);
        //                    $city_info = area_helper::get_area_info(@$store_info['city_id']);
        //                    $county_info = area_helper::get_area_info(@$store_info['district_id']);
        //                    $store_info['province'] = @$province_info['area'];
        //                    $store_info['city'] = @$city_info['area'];
        //                    $store_info['county'] = @$county_info['area'];
        //                } else {
        //                    $store_info['province'] = '';
        //                    $store_info['city'] = '';
        //                    $store_info['county'] = '';
        //                }
        //
        //                $record_list[$k]['store_info'] = $store_info;
        //            }
        //        }

        //        Response::assign('record_list', $record_list);
        Response::assign('device_history', $device_history);
        Response::assign('device_no', $device_no);
        Response::display('admin/device_no_record.html');

    }
}
